package com.cortex.pineapple.view;

import com.cortex.pineapple.R;

import android.R.attr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class CortexTextView extends TextView {

	TYPE_FACE typeFace = TYPE_FACE.REGULAR;

	public CortexTextView(Context context) {
		super(context);
		init(null);
	}
	public CortexTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}
	public CortexTextView(Context context, AttributeSet attrs,int style) {
		super(context, attrs,style);
		init(attrs);
	}


	private void init(AttributeSet attrs){
//		// change the font of textView
		setStyleFace(Typeface.NORMAL);
		if(attrs != null){
			TypedArray a = getContext().obtainStyledAttributes(attrs,
					R.styleable.CortexTextView);

			final int N = a.getIndexCount();
			for (int i = 0; i < N; ++i)
			{
				int attr = a.getIndex(i);
				switch (attr)
				{
				case R.styleable.CortexTextView_fontType:
					int typeFont = a.getInt(attr,TYPE_FACE.REGULAR.ordinal());
					setFont(TYPE_FACE.values()[typeFont]);
					break;
				}
			}
			a.recycle();
		}
	}

	public void setStyleFace(int styleFace){
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(),getFont());
		setTypeface(tf,styleFace);
	}
	private String getFont(){
		String font = "";
		switch(typeFace){
		case LIGHT:
			font = "fonts/robotoLight.ttf";
			break;
		case REGULAR:
			font = "fonts/robotoRegular.ttf";
			break;
		default:
			font = "fonts/robotoRegular.ttf";
			break;

		}
		return font;
	}
	public void setFont(TYPE_FACE typeFace){
		this.typeFace = typeFace;
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(),getFont());
		setTypeface(tf);
	}
	public void setTextWithUnderLine(String text){
		// add underline
		SpannableString content = new SpannableString(text);
		content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
		super.setText(content);
	}
	public enum TYPE_FACE{
		REGULAR,
		LIGHT
	}
}
